package com.hxd.otc.module.login.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BasePhoneCodeActivity;
import com.hxd.otc.module.login.adapter.PhoneCodeAdapter;
import com.hxd.otc.module.login.bean.PhoneCodeBean;
import com.hxd.otc.module.my.contract.PhoneCodeContract;
import com.hxd.otc.mvp.login.PhoneCodePresenter;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.widget.SearchView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/5 14:37
 * 描  述 :  ${电话区号获取}
 */
public class PhoneCodeActivity extends BasePhoneCodeActivity<PhoneCodePresenter> implements PhoneCodeContract.View {

    private PhoneCodeAdapter mCodeAdapter;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.searchView)
    SearchView mSearchView;
    boolean isAddBank;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone_code;
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {

        if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isAddBank")) {//添加银行卡进来的
            mSearchView.setHint(getString(R.string.search_country_or_region));
            isAddBank = true;
            mPresenter.gainPhoneCode(getAssets(), Constant.Login.PHONE_CODE_PATH);
        } else {// 登录页面进来的
//            mPresenter.gainPhoneCode(getAssets(), Constant.Login.PHONE_CODE_PATH);
            mPresenter.getZoneCode();
            inflateAdapter(null);
        }
    }

    @Override
    protected void initEvent() {
        mSearchView.setAutoSearch(true);
        mSearchView.setOnSearchListener(new SearchView.OnSearchListener() {
            @Override
            public void onSearch(String content) {
                filter(content);
            }
        });
    }

    @Override
    protected PhoneCodePresenter createPresenter() {
        return new PhoneCodePresenter(this);
    }

    @Override
    public void resultPhoneCode(List<PhoneCodeBean> data) {
        inflateAdapter(data);
    }

    @Override
    public void setZoneCode(List<PhoneCodeBean> data) {
        LogUtils.e("======1=data=="+data);
        if (data != null && data.size() > 0) {
            LogUtils.e("====2===data=="+data.size());
            mCodeAdapter.setmData(data);
        }
    }

    private void inflateAdapter(List<PhoneCodeBean> data) {
        if (mCodeAdapter == null) {
            mCodeAdapter = new PhoneCodeAdapter(PhoneCodeActivity.this);
            if (data != null) {
                mCodeAdapter.setmData(data);
            }
            mCodeAdapter.setAddBank(isAddBank);
            mRecyclerView.setAdapter(mCodeAdapter);
            mCodeAdapter.setOnItemClickListener(new PhoneCodeAdapter.OnItemClickListener() {
                @Override
                public void itemClick(PhoneCodeBean phoneCode) {
                    setPhoneCodeResult(isAddBank ? phoneCode.getChineseName() : phoneCode.getCode());
                }
            });
        } else {
            mCodeAdapter.notifyDataSetChanged();
        }
    }

    private void filter(String key) {
        List<PhoneCodeBean> data = mPresenter.getData();
        if (data != null && data.size() <= 0)
            return;
        if (TextUtils.isEmpty(key)) {
            mCodeAdapter.setNewData(data);
            return;
        }

        ArrayList<PhoneCodeBean> newData = new ArrayList<>();
        for (PhoneCodeBean phoneCodeBean : data) {
            if ((isAddBank ? phoneCodeBean.getChineseName().contains(key) : phoneCodeBean.getChineseName().contains(key)) || phoneCodeBean.getCode().contains(key)) {
                newData.add(phoneCodeBean);
            }
        }
        mCodeAdapter.setNewData(newData);
    }

}
